<?php

namespace App\Transformers\Admin;


use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;

class ExaminationTransformer extends TransformerAbstract
{

    protected $availableIncludes = ['category', 'subject', 'user'];

    /**
     * 考试安排的转换层
     * @param  $examination
     * @return array
     */
    public function transform($examination)
    {

        return [
            'id' => $examination->id,
            'category_id' => $examination->category_id,
            'name' => $examination->name,
            'start_time' => $examination->start_time,
            'over_time' => $examination->over_time,
            'timer' => $examination->timer,
            'peoples' => count(explode(',', trim($examination->peoples, ','))),
            'subject_id' => $examination->subject_id,
            'status' => $examination->status,
            'user_id' => $examination->user_id
        ];
    }


    /**
     * 返回额外的分类数据
     * @param  $examination
     * @return Item
     */
    public function includeCategory($examination)
    {
        return $this->item($examination->category, new class extends TransformerAbstract {
            public function transform($category)
            {
                return [
                    'id' => $category->id ?? '',
                    'name' => $category->name ?? '',
                ];
            }
        });
    }

    /**
     * 返回额外的科目数据
     * @param  $examination
     * @return Item
     */
    public function includeSubject($examination)
    {
        return $this->item($examination->subject, new class extends TransformerAbstract {
            public function transform($category)
            {
                return [
                    'id' => $category->id ?? '',
                    'name' => $category->name ?? '',
                ];
            }
        });
    }

    /**
     * 返回额外的用户数据
     * @param $examination
     * @return Item
     */
    public function includeUser($examination)
    {
        return $this->item($examination->user, new class extends TransformerAbstract {
            public function transform($user)
            {
                return [
                    'id' => $user->id ?? '',
                    'username' => $user->username ?? '',
                ];
            }
        });
    }
}

